package caseManagerBusiness;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.GetPageCount;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Objects;

/**
 * Created by yangxianming on 2019/1/2.
 * 返回测试记录
 * type 和 caseId都可以为空
 */
public class LoadReport implements TestServerBusinessImpl {
    //每页总数
    final Integer everyPage = 15;
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        GetPageCount getPageCount = new GetPageCount(myConfig);
        ReturnJson returnJson = new ReturnJson();
        //读取请求参数
        String action = request.getParameter("action");
        if (Objects.equals("apiReport", action)){
            String type = request.getParameter("type");
            String caseId = request.getParameter("caseId");
            String date = request.getParameter("date");
            String batchNo = request.getParameter("batchNo");

            //判断参数是否为空
            String whereType = "", whereCaseid = "", whichDate = "", whereBatchNo = "", limit = "";
            if (!Objects.equals(type, null) && !Objects.equals(type, "")){
                whereType =  " AND p.type=\"" + type + "\" ";
            }
            if (!Objects.equals(caseId, null) && !Objects.equals(caseId, "")){
                whereCaseid = " AND p.caseId=\"" + caseId + "\" ";
            }
            if (!Objects.equals(date, null) && !Objects.equals(date, "")){
                whichDate = " AND p.updatetime LIKE \"" + date + "%\" ";
            }
            if (!Objects.equals(batchNo, null) && !Objects.equals(batchNo, "")){
                whereBatchNo = " AND p.batchnumber=\"" + batchNo + "\" ";
            }
            //数据库查询
            SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
            String select = "SELECT p.batchnumber, c.caseName, p.result, p.updatetime, p.id,p.testResult,p.expectResult ";
            String from = "FROM testreport p, testcase c ";
            String where = "where c.id = p.caseId" + whereType + whereCaseid + whichDate + whereBatchNo + " ORDER BY p.updatetime DESC;";
            String sqlWord = select + from + where;
//        String sqlWord = "SELECT c.caseName, p.result, p.createtime, p.id,p.log FROM testreport p, testcase c where c.id = p.caseId  AND p.type=\"3\" AND p.createtime >= '2019-08-01' AND p.createtime <= '2019-09-01' ORDER BY p.createtime DESC;";
            System.out.println(sqlWord);
            SqlReturnModel sqlReturnModel = sqlDispatch.selectWord(sqlWord);

            if (sqlReturnModel.getIsSuccess()){
                returnJson.setApiStatus(1);
                JSONObject resultObj = new JSONObject();
                JSONArray resultArr = new JSONArray();
                for (ArrayList<String> arrayLists : sqlReturnModel.getSelectReturnData()){
                    JSONArray oneResult = new JSONArray();
                    oneResult.add(arrayLists.get(0));
                    oneResult.add(arrayLists.get(1));
                    oneResult.add(arrayLists.get(2));
                    oneResult.add(arrayLists.get(3));
                    oneResult.add(arrayLists.get(4));
                    oneResult.add(arrayLists.get(5));
                    oneResult.add(arrayLists.get(6));
                    resultArr.add(oneResult);
                }
                resultObj.put("resultArr", resultArr);

                //获取页面总数
                select = "SELECT count(*) ";
                String countSql = select + from + where + ";";
                Integer count = getPageCount.getCount(countSql);
                Integer remained = count % everyPage > 0 ? 1 : 0;
                Integer pageCount = count/everyPage + remained;
                resultObj.put("pageCount", pageCount);

                returnJson.setResult(resultObj);
            }else {
                returnJson.setApiStatus(0);
                returnJson.setErrorMsg(sqlReturnModel.getFailingReason());
            }
        }

        if (Objects.equals("UIReport", action)){
            String type = request.getParameter("type");
            String caseId = request.getParameter("caseId");
            String date = request.getParameter("date");
            String batchNo = request.getParameter("batchNo");
            String toPage = request.getParameter("toPage");

            //判断参数是否为空
            String whereType = "", whereCaseid = "", whichDate = "", whereBatchNo = "", limit = "";
            if (!Objects.equals(type, null) && !Objects.equals(type, "")){
                whereType =  " AND p.type=\"" + type + "\" ";
            }
            if (!Objects.equals(caseId, null) && !Objects.equals(caseId, "")){
                whereCaseid = " AND p.caseId=\"" + caseId + "\" ";
            }
            if (!Objects.equals(date, null) && !Objects.equals(date, "")){
                whichDate = " AND p.updatetime LIKE \"" + date + "%\" ";
            }
            if (!Objects.equals(batchNo, null) && !Objects.equals(batchNo, "")){
                whereBatchNo = " AND p.batchnumber=\"" + batchNo + "\" ";
            }
            if (!Objects.equals(toPage, null) && !Objects.equals(toPage, "")){
                limit = getPageCount.getLimit(Integer.valueOf(toPage), everyPage);
            }

            SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
            String select = "SELECT c.caseName, p.result, p.updatetime, p.id, p.log ";
            String from = "FROM testreport p, testcase c ";
            String where = "where c.id = p.caseId" + whereType + whereCaseid + whichDate + whereBatchNo + " ORDER BY p.updatetime DESC ";
            String sqlWord = select + from + where + limit + ";";
            System.out.println(sqlWord);
            SqlReturnModel sqlReturnModel = sqlDispatch.selectWord(sqlWord);

            if (sqlReturnModel.getIsSuccess()){
                returnJson.setApiStatus(1);
                JSONObject resultObj = new JSONObject();
                JSONArray resultArr = new JSONArray();
                for (ArrayList<String> arrayLists : sqlReturnModel.getSelectReturnData()){
                    JSONArray oneResult = new JSONArray();
                    oneResult.add(arrayLists.get(0));
                    oneResult.add(arrayLists.get(1));
                    oneResult.add(arrayLists.get(2));
                    oneResult.add(arrayLists.get(3));
                    oneResult.add(arrayLists.get(4));
                    resultArr.add(oneResult);
                }
                resultObj.put("resultArr", resultArr);

                //获取页面总数
                select = "SELECT count(*) ";
                String countSql = select + from + where + ";";
                Integer count = getPageCount.getCount(countSql);
                Integer remained = count % everyPage > 0 ? 1 : 0;
                Integer pageCount = count/everyPage + remained;
                resultObj.put("pageCount", pageCount);

                returnJson.setResult(resultObj);
            }else {
                returnJson.setApiStatus(0);
                returnJson.setErrorMsg(sqlReturnModel.getFailingReason());
            }

        }

        if (Objects.equals("pyPageCount", action)){

        }

        if (Objects.equals("UIPageCount", action)){

        }



        return returnJson.toString();
    }
}
